package com.galleryviewpager.i_bezier;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 三阶贝塞尔曲线
 */
public class ThreeBezierCurveView extends View {
    private static final String TAG = "ThreeBezierCurveView";

    private boolean controlLeftOrNot = true;

    private Paint mPaint;

    private int controlX1, controlX2, controlY1, controlY2;

    private int mWidth, mHeight;
    private float[] lines = new float[12];
    private Path mPath;

    public ThreeBezierCurveView(Context context) {
        this(context, null);
    }

    public ThreeBezierCurveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ThreeBezierCurveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(10f);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);

        mPath = new Path();


    }

    public void setControlLeftOrNot(boolean controlLeftOrNot) {
        this.controlLeftOrNot = controlLeftOrNot;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        mWidth = widthMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(widthMeasureSpec) : 400;

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        mHeight = heightMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightMeasureSpec) : 400;

        setMeasuredDimension(mWidth, mHeight);

        controlX1 = mWidth / 4;
        controlX2 = mWidth / 4 * 3;

        controlY1 = controlY2 = mHeight / 4;

        lines[0] = 0;
        lines[1] = mHeight / 2;
        lines[2] = controlX1;
        lines[3] = controlY1;

        lines[4] = controlX1;
        lines[5] = controlY1;
        lines[6] = controlX2;
        lines[7] = controlY2;

        lines[8] = controlX2;
        lines[9] = controlY2;
        lines[10] = mWidth;
        lines[11] = mHeight / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawLines(lines, mPaint);

        mPath.reset();
        mPath.moveTo(0, mHeight / 2);
        mPath.cubicTo(lines[2], lines[3], lines[6], lines[7], lines[10], lines[11]);
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                int currentx = (int) event.getX();
                int currenty = (int) event.getY();
                if (controlLeftOrNot) {
                    controlX1 = currentx;
                    controlY1 = currenty;
                    lines[4] = lines[2] = currentx;
                    lines[5] = lines[3] = currenty;
                } else {
                    controlX2 = currentx;
                    controlY2 = currenty;
                    lines[6] = lines[8] = currentx;
                    lines[7] = lines[9] = currenty;
                }
                postInvalidate();
                break;
        }
        return super.onTouchEvent(event);
    }
}
